﻿using AntdUI;
using CardRegisterCollection.BLL;
using CardRegisterCollection.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Menu = CardRegisterCollection.Model.Menu;

namespace _9jCardRegisterCollection.Views
{
    public partial class RoleEdit : UserControl
    {
        AntdUI.Window window;

        List< Menu> menus = CurrentLoginInfo.Menus; 

        CardRegisterCollection.BLL.Role roleBll = new CardRegisterCollection.BLL.Role(); 
        CardRegisterCollection.BLL.RoleMenu roleMenuBll = new CardRegisterCollection.BLL.RoleMenu();
        CardRegisterCollection.Model.Role role = new CardRegisterCollection.Model.Role();

        List<int> menuIds = new List<int>();
        string roleid  ;
        public RoleEdit(AntdUI.Window _window, string RoleID)
        {
            window = _window;
            roleid = RoleID;
            InitializeComponent(); 
            role = roleBll.GetModel(roleid);
            InitData(); 
        }

        private void InitData()
        { 
            this.txtRoleName.Text = role.name;
            InitTree();
        }

        private void InitTree()
        {
            treMenu.Items.Clear();
            foreach (Menu menu in menus)
            {
                if (menu.ParentId == "0")
                {  
                    TreeItem treeItem = new TreeItem();
                    treeItem.Text =menu.Title;
                    treeItem.ID = menu.ID.ToString();
                    treMenu.Items.Add(treeItem);
                    for(int i = 0;i<menus.Count;i++)
                    {
                       if( menus[i].ParentId == menu.ID.ToString())
                        {
                            TreeItem treeItem1 = new TreeItem();
                            treeItem1.Text = menus[i].Title;
                            treeItem1.ID = menus[i].ID.ToString();
                            treMenu.Items[treMenu.Items.Count - 1].Sub.Add(treeItem1);
                        }
                    }
                } 
            }
        }

         
        private void RoleEdit_Load(object sender, EventArgs e)
        { 
            
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            menuIds.Clear();
            foreach (TreeItem item in treMenu.Items)
            {
                if (item.CheckState !=CheckState.Unchecked)
                {
                    menuIds.Add(int.Parse( item.ID));
                    for(int i =0;i<item.Sub.Count;i++)
                    {
                        if (item.Sub[i].CheckState == CheckState.Checked)
                        {
                            menuIds.Add(int.Parse(item.Sub[i].ID));
                        }
                    }
                }
            }
            if(menuIds.Count == 0) { AntdUI.Message.warn(window, "未选择授予权限", autoClose: 1);return; }
            //清空原有权限
            roleMenuBll.DeleteMenuItemByRoleId(roleid);
            //授予新权限
            string loginName = CurrentLoginInfo.UserInfo.UserName;
            for(int i = 0; i<menuIds.Count;i++)
            {
                CardRegisterCollection.Model.RoleMenu roleMenu = new CardRegisterCollection.Model.RoleMenu();
                roleMenu.RoleId = roleid;
                roleMenu.ID= Guid.NewGuid().ToString();
                roleMenu.MenuId = menuIds[i];
                roleMenu.CreatedBy = loginName;
                roleMenu.CreatedAt = DateTime.Now; 
                roleMenu.DeleteFlag = false;
                roleMenuBll.Add(roleMenu); 
            }
            AntdUI.Message.success(window, "授权成功", autoClose: 1);
        }
    }
}
